﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;

namespace Thksoft.UserCotrols
{
    public partial class Pagination : System.Web.UI.UserControl
    {
        #region 成员变量

        private GridView _targetGridView;
        public int _rowsPerPage;
        private int _currentPage;
        private int _pageCount;
        private int _rowsCount;
        private object _dataSource = null;
        private bool _isDataBindOnLoad = true;

        #endregion 成员变量

        #region 属性

        /// <summary>
        /// 获取或设置表格控件的实例，该表格控件的数据内容将受分页控件的控制。
        /// </summary>
        [Description("无说明"), Category("获取或设置表格控件的实例，该表格控件的数据内容将受分页控件的控制"), DefaultValue("")]
        public GridView GridViewSource
        {
            get
            {
                return _targetGridView;
            }
            set
            {
                _targetGridView = value;
            }
        }

        /// <summary>
        /// 获取或设置分页控件的数据源。
        /// </summary>
        [Description("无说明"), Category("获取或设置分页控件的数据源"), DefaultValue("")]
        public object DataSource
        {
            get
            {
                return _dataSource;
            }
            set
            {
                _dataSource = value;
            }
        }

        /// <summary>
        /// 获取或设置每页行数。
        /// </summary>
        [Description("无说明"), Category("获取或设置每页行数"), DefaultValue("")]
        public int RowsPerPage
        {
            get
            {
                if (HiddenField.Count == 0)
                {
                    if (_rowsPerPage == 0)
                    {
                        _rowsPerPage = 50;
                    }

                    return _rowsPerPage;
                }
                else
                {
                    _rowsPerPage = int.Parse(HiddenField["RowPerValue"].ToString());
                    return _rowsPerPage;
                }
            }
            set
            {
                if (value == 0)
                {
                    _rowsPerPage = 50;
                }
                else
                {
                    _rowsPerPage = value;
                }
            }
        }

        /// <summary>
        /// 获取或设置当前页序号。
        /// </summary>
        [Description("无说明"), Category("获取或设置当前页序号"), DefaultValue("")]
        public int CurrentPage
        {
            get
            {
                if (string.IsNullOrEmpty(txtCurrentPage.Text))
                {
                    _currentPage = 1;
                    txtCurrentPage.Text = _currentPage.ToString();
                    return _currentPage;
                }
                else
                {
                    _currentPage = int.Parse(txtCurrentPage.Text);
                    return _currentPage;
                }
            }
            set
            {
                if (string.IsNullOrEmpty(txtCurrentPage.Text))
                {
                    if (value == 0)
                    {
                        _currentPage = 1;
                        txtCurrentPage.Text = _currentPage.ToString();
                    }
                    else
                    {
                        _currentPage = value;
                        txtCurrentPage.Text = _currentPage.ToString();
                    }
                }
                else
                {
                    _currentPage = int.Parse(txtCurrentPage.Text);
                }
            }
        }

        /// <summary>
        /// 获取或设置总页数。
        /// </summary>
        [Description("无说明"), Category("获取或设置总页数"), DefaultValue("")]
        public int PageCount
        {
            get
            {
                return _pageCount;
            }
            set
            {
                _pageCount = value;
            }
        }

        /// <summary>
        /// 获取或设置数据总行数。
        /// </summary>
        [Description("无说明"), Category("获取或设置数据总行数"), DefaultValue("")]
        public int RowsCount
        {
            get
            {
                return _rowsCount;
            }
            set
            {
                _rowsCount = value;
                lblRowCount.Text = _rowsCount.ToString();
            }
        }

        /// <summary>
        /// 获取或设置数据总行数。
        /// </summary>
        [Description("无说明"), Category("获取或设置是否在OnLoad事件触发时数据绑定到GridView"), DefaultValue("")]
        public bool IsDataBindOnLoad
        {
            get
            {
                return _isDataBindOnLoad;
            }
            set
            {
                _isDataBindOnLoad = value;
            }
        }

        #endregion 属性

        #region 方法

        /// <summary>
        /// 将指定的数字字符串转换为整型类型，如果指定字符串不能转换成整型则返回默认值。
        /// </summary>
        /// <param name="s">需要被转换字符串。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>转换后整型值。</returns>
        private int ToInt(string s, int defaultValue)
        {
            try
            {
                int n = Convert.ToInt32(s);
                if (n <= 0)
                {
                    n = defaultValue;
                }
                return n;
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 提供外部绑定
        /// </summary>
        public void Bind()
        {
            if (DataSource != null && RowsCount >= 0)
            {
                decimal rowCountDecimal = _rowsCount;
                PageCount = Convert.ToInt32(Decimal.Ceiling(rowCountDecimal / _rowsPerPage));

                _targetGridView.DataSource = DataSource;
                _targetGridView.DataBind();
            }
        }

        #endregion 方法

        #region 事件

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            OnDataBinding(new EventArgs());
        }

        #endregion 事件
    }
}